package com.service.system.system.impl;

import com.dao.SystemGlobalConfigMapper;
import com.entity.system.SystemGlobalConfig;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.system.system.SystemConfigService;
import com.util.Const;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemConfigServiceImpl implements SystemConfigService {

    @Autowired
    SystemGlobalConfigMapper systemGlobalConfigMapper;

    @Override
    public Map<String, Object> findSystenConfigList(Map<String,Object> map) {
        Map<String,Object> map1 = new HashMap<>();
        int pageSize = 10;
        int pageNum = 1;
        if(StringUtils.isNotEmpty((String) map.get("pageSize"))){
            pageSize = Integer.parseInt(map.get("pageSize")+"");
        }
        if(StringUtils.isNotEmpty((String) map.get("pageNum"))){
            pageNum = Integer.parseInt(map.get("pageNum")+"");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<SystemGlobalConfig> systemGlobalConfigs = systemGlobalConfigMapper.findListByMap(map);
        PageInfo<SystemGlobalConfig> pageInfo = new PageInfo<>(systemGlobalConfigs);
        map1.put("code", Const.SUCCESS_CODE);
        map1.put("message","查询成功");
        map1.put("systemGlobalConfigs",pageInfo.getList());
        map1.put("page",pageInfo);
        return map1;
    }

    @Override
    public Map<String, Object> updateConfig(SystemGlobalConfig systemGlobalConfig) {
        Map<String,Object> map = new HashMap<>();
        systemGlobalConfig.setModifyTime(new Date());
        int num = systemGlobalConfigMapper.updateByPrimaryKeySelective(systemGlobalConfig);
        if(num == 1){
            map.put("code",Const.SUCCESS_CODE);
            map.put("message","修改成功");
        }else {
            map.put("code",Const.ERROR_CODE);
            map.put("message","修改失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> addConfig(SystemGlobalConfig systemGlobalConfig) {
        Map<String,Object> map = new HashMap<>();
        systemGlobalConfig.setCreateTime(new Date());
        int num = systemGlobalConfigMapper.insert(systemGlobalConfig);
        if(num == 1){
            map.put("code",Const.SUCCESS_CODE);
            map.put("message","添加成功");
        }else {
            map.put("code",Const.ERROR_CODE);
            map.put("message","添加失败");
        }
        return map;
    }
}
